<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2016/12/15
 * Time: 10:52
 */

namespace App\Services;
use App\Services\Algo\CancerTestingAlgo;
class TestingXjbAssistHelper
{
    /**新健保接口
     * @param $tid
     * @param $answerStr
     * @param $typeFourExpress
     * @return array
     */
   public function calculate($tid, $answerStr,$typeFourExpress){
        $result = [];
        $instance = new CancerTestingAlgo($tid,$answerStr,$typeFourExpress);
        $result['totalScore'] = $instance->calculate();
        $result['finalAnswerStr'] = $instance->resultToStr();
        return $result;
    }


    /**辅助BMI，获取对应题号的bmi 和 height
     * @param $finalAnswers_arr
     * @param $tid
     * @return array
     */
    public function weightparameter($finalAnswers_arr,$tid){
        $data=[];
       switch($tid){
           case 6:
               $data['bmi']=$this->getValue($finalAnswers_arr,16,'weight');
               $data['height']=$this->getValue($finalAnswers_arr,3,'answer');
               $data['weigntindex'] = 4;
               break;
           case 7:
               $data['bmi']=$this->getValue($finalAnswers_arr,10,'weight');
               $data['height']=$this->getValue($finalAnswers_arr,3,'answer');
               $data['weigntindex'] = 4;
               break;
       }
        return $data;
    }

    /***辅助吸烟，获取对应题号的bmi 和 height
     * @param $finalAnswers_arr
     * @param $tid
     * @return array
     */
    public function smokingparameter($finalAnswers_arr,$tid){
        $data=[];
        switch($tid){
            case 7:
                $data['smoking'] = $this -> getValue($finalAnswers_arr,6,'answer');
                $data['smokingindex']=6;
                break;
            case 9:
                $data['smoking'] = $this -> getValue($finalAnswers_arr,6,'answer');
                $data['smokingindex']=6;
                break;
            case 10:
                $data['smoking'] = $this -> getValue($finalAnswers_arr,6,'answer');
                $data['smokingindex']=6;
                break;
            case 11:
                $data['smoking'] = $this -> getValue($finalAnswers_arr,6,'answer');
                $data['smokingindex']=6;
                break;
        }
        return $data;
    }

    /**根据题号获取相应的权重或者值
     * @param $answers
     * @param $index
     * @param $valueType
     * @return null
     */
    public function getValue($answers,$index,$valueType){
        $value = null;
        foreach($answers as $k => $v){
            if($v['index'] == $index){
                $value = $answers[$k][$valueType];
            }
        }
        return $value;
    }

    /**将答案数组组装成字符串
     * @param $answers
     * @return string
     */
    public function arrToStr($answers){
        $arr = [];
        foreach($answers as $k => $v){
            $arr[0]=$v['index'];
            $arr[1]=$v['weight'];
            $arr[2]=$v['answer'];
            $str=implode(":",$arr);
            $answers[$k]=$str;
        }
        $answerStr = implode("##",$answers);
        return $answerStr;
    }


    /***将答案字符串转换为数组
     * @param $answerStr
     * @return array
     */
    public function strToArr($answerStr){
        $answers = [];
        $answerArr = explode('##',$answerStr);
        if(count($answerArr)>0){
            foreach ($answerArr as $answer){
                $singleArr = explode(':',$answer);
                if(count($singleArr)>0){
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers,$obj);
                }
            }
        }
        return $answers;
    }


    /**返回回答性别
     * @param $answerArr
     * @return null
     */
    public function getSex($answerArr){
        $sexres = [];
        foreach($answerArr as $k => $v){
            if($v['index'] == 1){
                $sexres['sex'] = $v['weight'];
                $sexres['index'] = $v['index'];
            }
        }
        return $sexres;
    }
}